package core;

import gitobject.Commit;
import stage.Index;

import java.io.File;

public class JitLog {
    /**
     * Print commit history starting from the latest commit.
     * @throws Exception
     */
    public static void getLogHistory() throws Exception {
        String lastCommit = new Commit().getLastCommit();
        if (lastCommit == null) {
            System.out.println("fatal: your current branch does not have any commits yet");
            return;
        }
        else {
            do {
                Commit currentCommit = Commit.deserialize(lastCommit);
                printLog(currentCommit);
                lastCommit = currentCommit.getParent();
            }
            while (!"".equals(lastCommit));
        }
    }

    /**
     * Print each commit info.
     * @param commit
     */
    public static void printLog(Commit commit) {
        System.out.println(commit.getFmt() + " " + commit.getKey());
        System.out.println("Author: " + commit.getAuthor());
        System.out.println("Date: " + commit.getTimestamp());
        System.out.println("\n\t" + commit.getMessage() + "\n");
    }

}
